package com.crm.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.crm.dao.NewsCategoryDao;
import com.crm.entity.NewsCategory;
import com.crm.service.NewsCategoryService;

@Service
public class NewsCategoryServiceImpl implements NewsCategoryService {

	@Autowired
	private NewsCategoryDao newsCategoryDao;

	@Override
	public List<NewsCategory> getNewsCategory() {
		return newsCategoryDao.findAll();
	}

	@Override
	public void addNewsCategory(NewsCategory newsCategory) {
		newsCategoryDao.save(newsCategory);
	}

	@Override
	public Page<NewsCategory> getNewsCategoryBycategory(String category,Integer pageIndex) {
		NewsCategory newsCategory =  new NewsCategory();
		if(StringUtils.isNotBlank(category)) {
			newsCategory.setCategory(category);
		}
		Example<NewsCategory> example = Example.of(newsCategory);
		
		PageRequest pageable = new PageRequest(pageIndex, 10);
		
		return newsCategoryDao.findAll(example, pageable);
	}
	
	
}
